package cn.edu.neusoft.yuejian4.yuejian_4.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.ActivityDetailsActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.TrainDetailActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Activity;



/**
 * Created by Lix on 2018/10/10.
 */

public class ActivityListAdapter extends RecyclerView.Adapter<ActivityListAdapter.ViewHolder> {

    private List<Activity> mActivity;
    private Context mContext;

    public ActivityListAdapter(Context mContext, List<Activity> mActivity){
        this.mContext = mContext;
        this.mActivity = mActivity;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public ActivityListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_cardview,parent,false);
        ActivityListAdapter.ViewHolder holder = new ActivityListAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ActivityListAdapter.ViewHolder holder, int position) {

        final Activity activity = mActivity.get(position);
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+activity.getPic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.actImg);

        holder.actName.setText(activity.getName());
        holder.actAddress.setText(activity.getAddress());
        holder.actImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ActivityDetailsActivity.class);
                intent.putExtra("act_id",activity.getId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mActivity.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView actImg;
        TextView actName;
        TextView actAddress;
        FrameLayout act_container;

        public ViewHolder(View view){
            super(view);
            actImg= (ImageView)view.findViewById(R.id.act_img);
            actName = (TextView)view.findViewById(R.id.act_name);
            actAddress = (TextView)view.findViewById(R.id.act_address);
            act_container = (FrameLayout)view.findViewById(R.id.act_container);
        }
    }
}
